package com.leapp.sk.viewdelegate.join

import android.text.Editable
import android.text.TextUtils
import android.text.TextWatcher
import android.view.View
import android.widget.ImageView
import android.widget.ListView
import com.example.mvp.delegate.BaseAppDelegate
import com.example.mvp.view.ContainsEmojiEditText
import com.leapp.sk.R
import com.leapp.sk.app.SKApplication
import com.leapp.sk.bean.SearchClassListEntity
import com.leapp.sk.inter.JoinClassInterface
import com.leapp.sk.inter.SearchInterface
import com.leapp.sk.ui.adapter.SearchClassAdapter

/**
 * 搜索班级视图托管层
 * @author  chaohaipeng  on 2018/7/11.
 */
class  SearchClassDelegate : BaseAppDelegate(){
    /**
     * 适配器
     */
    var mAdapter:SearchClassAdapter?=null
    /**
     * 是否是宝贝档案
     */
    var isBaby: Boolean?=false
    /**
     * 搜索接口
     */
    var  searchInterface: SearchInterface?=null
    /**
     * 加入班级接口
     */
    var  joinClassInterface: JoinClassInterface?=null

    override fun initWidget() {
    }

    override fun initLayoutRes(): Int {
        return R.layout.activity_search_class
    }
    /**
     * 初始化适配器
     */
    fun initAdapter(listSearch:List<SearchClassListEntity>, isBaby:Boolean?){
        this.isBaby=isBaby
        val lv_asc_search_content= getView<ListView>(R.id.lv_asc_search_content)
        mAdapter= SearchClassAdapter(getActivity(), listSearch)
        lv_asc_search_content.adapter=mAdapter

    }

    fun setSearchInterfaces(searchInterface:SearchInterface){
        this.searchInterface=searchInterface
    }
    fun setJoinClassInterfaces(joinClassInterface: JoinClassInterface){
        this.joinClassInterface=joinClassInterface
    }




    /**
     * 刷新适配器
     */
    fun notifyDataSetChanged(){
        mAdapter?.notifyDataSetChanged()
    }
    /**
     * 设置清理按钮隐显示
     */
    fun setClearDatavisible(){
        val iv_asc_clear= getView<ImageView>(R.id.iv_asc_clear)
        val et_asc_search_content= getView<ContainsEmojiEditText>(R.id.et_asc_search_content)
        et_asc_search_content.addTextChangedListener(object : TextWatcher {
            override fun afterTextChanged(p0: Editable?) {
                val toString = p0?.toString()
                val compareTo = toString?.length!!.compareTo(0)
                if(compareTo==0){
                    iv_asc_clear.visibility= View.INVISIBLE
                }else{
                    iv_asc_clear.visibility= View.VISIBLE
                }
                if (!TextUtils.isEmpty(toString)){
                    SKApplication.getInstance()?.mHandler?.postDelayed(Runnable {
                        searchInterface?.searchData(toString)
                    },200)
                }

            }
            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        })
    }
    /**
     * 清理内容
     */
    fun clearContent(){
        val et_asc_search_content= getView<ContainsEmojiEditText>(R.id.et_asc_search_content)
        et_asc_search_content.setText("")
    }
    /**
     * 子条目点击事件
     */
    fun itemEvent(){
        val lv_asc_search_content= getView<ListView>(R.id.lv_asc_search_content)
        lv_asc_search_content.setOnItemClickListener { adapterView, view, i, l ->
            joinClassInterface?.joinClass(mAdapter?.mList?.get(i))


        }
    }


}